package com.lemon.smartsafetynew.ui.fragment.hidden;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.TextView;

import com.lemon.smartsafety.R;
import com.lemon.smartsafety.app.AppService;
import com.lemon.smartsafetynew.ui.fragment.base.BaseFragment;
import com.lemon.smartsafetynew.entity.event.hidden.ClickEvent;
import com.lemon.smartsafetynew.entity.hidden.SafeHiddenBo;
import com.lemon.smartsafetynew.entity.hidden.SafeHiddenDownloadVo;
import com.lemon.smartsafetynew.util.constants.KeyConstants;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Created by Andrew on 2017/9/10.
 */

public class SafeHiddenProcessTabOneFragment extends BaseFragment {

    @Bind(R.id.one_btn)
    TextView oneBtn;
    @Bind(R.id.two_btn)
    TextView twoBtn;
    @Bind(R.id.three_btn)
    TextView threeBtn;

    private static SafeHiddenDownloadVo safeHiddenDownloadVo;

    private int mCurrentDrawId;

    @Override
    protected void getBaseArguments() {

        Bundle bundle = getArguments();
        if (bundle != null) {
            if (bundle.containsKey(KeyConstants.KEY_SAFE_HIDDEN_DOWNLOAD_VO)) {
                safeHiddenDownloadVo = (SafeHiddenDownloadVo) bundle.getSerializable(KeyConstants.KEY_SAFE_HIDDEN_DOWNLOAD_VO);
            }
        }
    }

    public static SafeHiddenProcessTabOneFragment newInstance(SafeHiddenDownloadVo data) {

        Bundle args = new Bundle();
        args.putSerializable(KeyConstants.KEY_SAFE_HIDDEN_DOWNLOAD_VO, data);

        SafeHiddenProcessTabOneFragment fragment = new SafeHiddenProcessTabOneFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected int getContentViewId() {
        return R.layout.fragment_process_tab_one;
    }

    @Override
    protected void initViews() {
        //initCommon();
        transactionSupportFragment(SafeHiddenProcessTabOneContentFragment.newInstance(safeHiddenDownloadVo));
    }

    private void initCommon() {
        if (SafeHiddenBo.Status.rectify.getValue().equals(safeHiddenDownloadVo.getStatus())) {
            select(R.id.one_btn);
            twoBtn.setEnabled(false);
            threeBtn.setEnabled(false);
            mCurrentDrawId = R.id.one_btn;
        } else if (SafeHiddenBo.Status.review.getValue().equals(safeHiddenDownloadVo.getStatus())) {
            disSelected(R.id.one_btn);
            select(R.id.two_btn);
            threeBtn.setEnabled(false);
            mCurrentDrawId = R.id.two_btn;
        } else if (SafeHiddenBo.Status.finish.getValue().equals(safeHiddenDownloadVo.getStatus())) {
            disSelected(R.id.one_btn);
            disSelected(R.id.two_btn);
            select(R.id.three_btn);
            mCurrentDrawId = R.id.three_btn;
        }
    }

    @OnClick({R.id.one_btn, R.id.two_btn, R.id.three_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.one_btn:

                disSelected(mCurrentDrawId);
                mCurrentDrawId = R.id.one_btn;
                select(mCurrentDrawId);
                AppService.getBus().post(new ClickEvent(SafeHiddenBo.Status.create.getDesc()));

                break;
            case R.id.two_btn:

                disSelected(mCurrentDrawId);
                mCurrentDrawId = R.id.two_btn;
                select(mCurrentDrawId);
                AppService.getBus().post(new ClickEvent(SafeHiddenBo.Status.rectify.getDesc()));

                break;
            case R.id.three_btn:

                disSelected(mCurrentDrawId);
                mCurrentDrawId = R.id.three_btn;
                select(mCurrentDrawId);
                AppService.getBus().post(new ClickEvent(SafeHiddenBo.Status.review.getDesc()));

                break;
        }
    }

    private void disSelected(int id) {
        switch (id) {
            case R.id.one_btn:
                oneBtn.setSelected(false);
                break;
            case R.id.two_btn:
                twoBtn.setSelected(false);
                break;
            case R.id.three_btn:
                threeBtn.setSelected(false);
                break;
        }
    }

    private void select(int id) {
        switch (id) {
            case R.id.one_btn:
                oneBtn.setSelected(true);
                break;
            case R.id.two_btn:
                twoBtn.setSelected(true);
                break;
            case R.id.three_btn:
                threeBtn.setSelected(true);
                break;
        }
    }

    /**
     * Fragment替换
     *
     * @param fragment
     */
    private void transactionSupportFragment(Fragment fragment) {
        getChildFragmentManager().beginTransaction().replace(R.id.content_layout, fragment).commit();
    }

}
